var globalData;

function initializeYelpReviews(zipCode) {
	try {
		
	var auth = { 
			  consumerKey: "RUpS6PU9Ei8sgA8wZaziIg", 
			  consumerSecret: "RBy_yuomJpdX32f1TG3TjNMaq08",
			  accessToken: "k6GQelVpDeKEgFCUPcLbDFbYGtV6Vev-",
			  accessTokenSecret: "7gF4Uoidu95J0HBbekbNYQ5AebE",
			  serviceProvider: { 
			    signatureMethod: "HMAC-SHA1"
			  }
			};

	var terms = 'food';
	var near = zipCode;

	var accessor = {
	  consumerSecret: auth.consumerSecret,
	  tokenSecret: auth.accessTokenSecret
	};

	parameters = [];
	parameters.push(['term', terms]);
	parameters.push(['location', near]);
	parameters.push(['callback', 'cb']);
	parameters.push(['oauth_consumer_key', auth.consumerKey]);
	parameters.push(['oauth_consumer_secret', auth.consumerSecret]);
	parameters.push(['oauth_token', auth.accessToken]);
	parameters.push(['oauth_signature_method', 'HMAC-SHA1']);

	var message = { 
	  'action': 'http://api.yelp.com/v2/search',
	  'method': 'GET',
	  'parameters': parameters 
	};

	OAuth.setTimestampAndNonce(message);
	OAuth.SignatureMethod.sign(message, accessor);

	var parameterMap = OAuth.getParameterMap(message.parameters);
	parameterMap.oauth_signature = OAuth.percentEncode(parameterMap.oauth_signature);
	
	$.ajax({
	  'url': message.action,
	  'data': parameterMap,
	  'cache': true,
	  'dataType': 'jsonp',
	  'jsonpCallback': 'cb',
	  'success': function(data, textStats, XMLHttpRequest) {
		globalData = data;
	    process(data);
	  },
	  'error': function(jqXHR, textStatus, errorThrown) {
		 alert("Exceeded Yelp API Usage");
	  }
	});	
		
	}
	catch (err) {
		alert("Exceeded Yelp API Usage");
	}
}

function process(data) {
	var categories = new Array();
	var titles = new Array();
	var images = new Array();
	var descriptions = new Array();	
	var imageWidths = new Array();
	var businesses = data.businesses;
	var postIds = new Array();
	var popularity = new Array();
	
	for (var i =0; i < businesses.length; i++) {
		categories.push("Restaurants");
		titles.push(businesses[i].name);
		if (typeof businesses[i].image_url === 'undefined') {
			images.push("http://s3-media3.ak.yelpcdn.com/assets/2/www/img/2d7ab232224f/developers/yelp_logo_100x50.png");
		}
		else {
			images.push(businesses[i].image_url);
		}
		
		var snipText = businesses[i].snippet_text.replace("", '"');
		
		descriptions.push("<div style='text-align: center;'>" + businesses[i].rating + " Stars</div>" + 
							snipText + " <br> Phone: " + businesses[i].phone);
		imageWidths.push("125");
		postIds.push(0);
		popularity.push(0);
	}
		
	appendPosts(categories, titles, images, descriptions, imageWidths, "true", postIds, popularity);	
	filter();
}
